package deyi.task;

import deyi.cache.AppCache;
import deyi.dao.DeviceDataDB;
import deyi.entity.DeviceEntity;
import deyi.entity.DeviceconfigEntity;
import deyi.entity.DevicedataEntity;
import deyi.log.AppLogger;
import deyi.nbtype.DeviceConfigInfo;
import deyi.nbtype.DeviceMetaData;
import deyi.utils.*;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.ejb.interceptor.SpringBeanAutowiringInterceptor;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.test.context.ContextLoader;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

@Component("dbprocessor")
public class DbProcessor {

    @Autowired
    private AppConfig appConfig;
    @Autowired
    private AppCache appCache;
    @Autowired
    private DeviceDataDB deviceDataDB;
    @Scheduled(fixedDelayString = "${taskPostData}000",initialDelay = 5000)
    @org.springframework.transaction.annotation.Transactional
    public void processData(){

        List<DeviceMetaData> metaDataList = appCache.getDeviceAppDataList().cloneList();
        if(metaDataList.size() == 0) return;
        List<DevicedataEntity> listDeviceData = new ArrayList<DevicedataEntity>();
        List<DeviceConfigInfo> listDeviceConfigInfo = new ArrayList<DeviceConfigInfo>();
        for(DeviceMetaData metaData:metaDataList){
            if(metaData.getDeviceData()[0] == 0x00){
               if(metaData.getDeviceData().length == 27){
                   //设备上报数据
                   DevicedataEntity devicedata = new DevicedataEntity();
                   devicedata.setDevicesn(metaData.getDeviceSn());
                   //设备类型
                   devicedata.setDevicetype(CommonUtil.byteToInt( metaData.getDeviceData()[1]));
                   //设备状态
                   devicedata.setDevicestatus(CommonUtil.byteToInt(metaData.getDeviceData()[2]));
                   //设备csq
                   devicedata.setCsq(CommonUtil.byteToInt(metaData.getDeviceData()[3]));
                   //设备rsrp
                   devicedata.setRsrp(CommonUtil.byteToInt(metaData.getDeviceData()[4]));
                   //设备电量
                   devicedata.setVim(Float.intBitsToFloat(CommonUtil.fromByteArray(CommonUtil.getCopyBytes(metaData.getDeviceData(),5,4))));
                   //参考电压
                   devicedata.setVref(Float.intBitsToFloat(CommonUtil.fromByteArray(CommonUtil.getCopyBytes(metaData.getDeviceData(),9,4))));

                   //压力
                   devicedata.setPressure(CommonUtil.byteArrayToInt(CommonUtil.getCopyBytes(metaData.getDeviceData(),13,4)));
                   //强磁
                   devicedata.setDevicemagnet(CommonUtil.byteToInt(metaData.getDeviceData()[17]));
                   //设备报警
                   devicedata.setDevicealarm(CommonUtil.byteToInt(metaData.getDeviceData()[18]));
                   //正向
                   devicedata.setDeviceforwardflow(CommonUtil.byteArrayToInt(CommonUtil.getCopyBytes(metaData.getDeviceData(),19,4)));
                   //反向
                   devicedata.setDeviceoppositeflow(CommonUtil.byteArrayToInt(CommonUtil.getCopyBytes(metaData.getDeviceData(),23,4)));

/**
                   //压力
                   devicedata.setPressure(CommonUtil.byteToInt(metaData.getDeviceData()[13]));
                   //强磁
                   devicedata.setDevicemagnet(CommonUtil.byteToInt(metaData.getDeviceData()[14]));
                   //设备报警
                   devicedata.setDevicealarm(CommonUtil.byteToInt(metaData.getDeviceData()[15]));
                   //正向
                   devicedata.setDeviceforwardflow(CommonUtil.byteToInt(metaData.getDeviceData()[16]));
                   //反向
                   devicedata.setDeviceoppositeflow(CommonUtil.byteToInt(metaData.getDeviceData()[17]));
*/
                   //时间
                   devicedata.setCreatedate(new Timestamp(metaData.getDeviceReportTime().getTime()));
                   //数据格式
                   devicedata.setMetadata(CommonUtil.byte2HexStr(metaData.getDeviceData()));
                   listDeviceData.add(devicedata);
               }else{
                   DevicedataEntity devicedata = new DevicedataEntity();
                   devicedata.setDevicesn(metaData.getDeviceSn());
                   devicedata.setDevicetype(CommonUtil.byteToInt( metaData.getDeviceData()[1]));
                   devicedata.setDevicestatus(CommonUtil.byteToInt(metaData.getDeviceData()[2]));
                   devicedata.setDevicemagnet(CommonUtil.byteToInt(metaData.getDeviceData()[3]));
                   devicedata.setDevicealarm(CommonUtil.byteToInt(metaData.getDeviceData()[4]));
                   devicedata.setDeviceforwardflow(CommonUtil.byteArrayToInt(CommonUtil.getCopyBytes(metaData.getDeviceData(),5,4)));
                   devicedata.setDeviceoppositeflow(CommonUtil.byteArrayToInt(CommonUtil.getCopyBytes(metaData.getDeviceData(),9,4)));
                   devicedata.setCreatedate(new Timestamp(metaData.getDeviceReportTime().getTime()));
                   devicedata.setMetadata(CommonUtil.byte2HexStr(metaData.getDeviceData()));
                   listDeviceData.add(devicedata);
               }
            }else{
                //配置应答
                if(metaData.getDeviceData().length == 7){
                    DeviceConfigInfo deviceConfigInfo = new DeviceConfigInfo();
                    deviceConfigInfo.setDeviceSn(metaData.getDeviceSn());
                    deviceConfigInfo.setDeviceStatus(metaData.getDeviceData()[6]);
                    deviceConfigInfo.setDeviceType(metaData.getDeviceData()[5]);
                    deviceConfigInfo.setRevConfigTime(metaData.getDeviceReportTime());
                    deviceConfigInfo.setConfigId(CommonUtil.byteArrayToInt(CommonUtil.getCopyBytes(metaData.getDeviceData(),1,4)));
                    listDeviceConfigInfo.add(deviceConfigInfo);
                }
            }
        }
        deviceDataDB.batchInsert(listDeviceData,listDeviceConfigInfo);

    }
}
